package com.primb.bench.base;

import android.app.Activity;
import android.app.Dialog;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.Window;
import android.widget.Toast;

import com.flyco.dialog.widget.NormalDialog;
import com.primb.baselibs.view.loading.SpotsDialog;
import com.primb.bench.R;
import com.zhy.autolayout.AutoLayoutActivity;

import java.util.ArrayList;

import butterknife.ButterKnife;


/**
 * activity基类
 */
public abstract class BaseActivity extends AutoLayoutActivity {

    /**
     * activity管理集合，退出时把activity销毁掉
     */
    public static ArrayList<Activity> activityList = new ArrayList<>();
    private SpotsDialog loadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityList.add(this);
        onBaseIntent();

        onBasePreLayout();
        /**
         * 去除actionbar，必须放在setContentView前面
         */
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(onBaseLayoutId(savedInstanceState));
        ButterKnife.bind(this);
        onBaseBindView();

        onLoadData();
    }

    /**
     * Intent进来的数据处理
     */
    protected void onBaseIntent() {

    }

    /**
     * 设置布局之前的处理
     */
    protected void onBasePreLayout() {

    }

    /**
     * 返回布局文件
     *
     * @return id
     */
    protected abstract int onBaseLayoutId(@Nullable Bundle savedInstanceState);

    protected abstract void onBaseBindView();

    /**
     * 加载数据
     */
    protected abstract void onLoadData();

    @Override
    protected void onStart() {
        super.onStart();
    }

    /**
     * 防止用户更改字体导致布局变形
     */
    @Override
    public Resources getResources() {
        Resources resources = super.getResources();
        Configuration config = new Configuration();
        config.setToDefaults();
        resources.updateConfiguration(config, resources.getDisplayMetrics());
        return resources;
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    /**
     * 程序退出
     */
    public static void exit() {
        for (Activity activity : activityList) {
            activity.finish();
        }
        activityList.clear();
//        杀死进程
        android.os.Process.killProcess(android.os.Process.myPid());
//        通知垃圾回收器回收垃圾
        System.gc();
//        退出
        System.exit(0);
    }

    public void showToast(String text) {
        if (text == null || TextUtils.isEmpty(text))
            return;
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
    }


    public NormalDialog showAlterDialogTwo(String title, String message) {
        NormalDialog dialog = new NormalDialog(this);
        dialog.widthScale(0.8f);
        dialog.style(NormalDialog.STYLE_TWO);
        dialog.titleLineColor(ContextCompat.getColor(this, R.color.app_main_gray));
        dialog.title(title);
        dialog.btnNum(2);
        dialog.content(message);
        dialog.dividerColor(ContextCompat.getColor(this, R.color.app_main_gray));
        dialog.show();
        return dialog;
    }

    public NormalDialog showAlterDialogOne(String title, String message) {
        NormalDialog dialog = new NormalDialog(this);
        dialog.widthScale(0.8f);
        dialog.style(NormalDialog.STYLE_TWO);
        dialog.titleLineColor(ContextCompat.getColor(this, R.color.app_main_gray));
        dialog.title(title);
        dialog.btnNum(1);
        dialog.content(message);
        dialog.dividerColor(ContextCompat.getColor(this, R.color.app_main_gray));
        dialog.btnText("确定");
        dialog.show();
        return dialog;
    }


    public void showLoadingDialog(String msg) {
        if (loadingDialog == null) {
            loadingDialog = new SpotsDialog(this, msg);
        } else {
            loadingDialog.setMessage(msg);
        }
        loadingDialog.show();
    }

    public void hideLoadingDialog() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }
}
